#include "Algorithm.h"
#include "Algorithm_types.h"
#include <chrono>
#include <thread>
#define STB_IMAGE_IMPLEMENTATION
#include <iostream>
#include <stb/stb_image.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TProtocol.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TFileTransport.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransport.h>
#include <thrift/transport/TTransportUtils.h>

int main() {
  int imgWidth, imgHeight, channels;
  unsigned char *testImg = stbi_load("/home/shenzhe/Code/ImageExample/d/12.png",
                                     &imgWidth, &imgHeight, &channels, 0);
  std::cout << "channels: " << channels << std::endl;
  std::shared_ptr<apache::thrift::transport::TTransport> socket(
      new apache::thrift::transport::TSocket("192.168.1.5", 14005));
  std::shared_ptr<apache::thrift::transport::TTransport> transport(
      new apache::thrift::transport::TBufferedTransport(socket));
  std::shared_ptr<apache::thrift::protocol::TProtocol> protocol(
      new apache::thrift::protocol::TBinaryProtocol(transport));
  AlgorithmIPC::AlgorithmClient client(protocol);

  transport->open();

  std::string ret;
  AlgorithmIPC::ImageData data;
  data.format = AlgorithmIPC::ImageFormat::GREY;
  data.width = imgWidth;
  data.height = imgHeight;
  data.data = std::string((char *)testImg, imgWidth * imgHeight);

  while (true) {
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    client.HandleImage(ret, data);
    std::cout << "Return: " << ret << std::endl;
  }

  transport->close();
}
